package engine.logic
{
	import engine.GameData;
	import engine.data.ModelLocator;
	import engine.event.CustomEvent;
	import engine.event.SceneEvent;
	import engine.flag.GameDefine;
	import engine.flag.PlayerActionFlag;
	import engine.logic.walk.WalkManager;
	import engine.net.event.CustomMsgEvent;
	import engine.net.messages.CustomToClientFlag;
	import engine.newUi.DuplicatesLogic;
	import engine.newUi.FunctionGuideUI;
	import engine.newUi.SitCrossleggedLogic;
	import engine.newUi.TipsWindowUI;
	import engine.newUi.WorldMapLogic;
	import engine.scene.SceneManager;
	import engine.scene.player.BasePlayerModel;
	import engine.ui.MainUIManager;
	import engine.ui.UIManage;
	
	import flash.events.MouseEvent;
	import flash.geom.Point;
	import flash.utils.setTimeout;

	/**
	 *	跨场景移动 
	 * @author lw
	 * 
	 */
	public class SpanSceneMoveLogic
	{
		private static var _self	:SpanSceneMoveLogic;
		
		private var _model			: ModelLocator;
		
		private var _npcModel		: BasePlayerModel;
		
		private var _roleModel		: BasePlayerModel;
		
		private var _isOver		:Boolean	=	false;
		
		private var _doorID		:String;
		
		public static function getInstance():SpanSceneMoveLogic
		{
			if(_self == null)
			{
				_self	=	new SpanSceneMoveLogic();
			}
			return _self;
		}
		
		public function SpanSceneMoveLogic()
		{
			_model = ModelLocator.getInstance();
			_model.addEventListener(String(CustomToClientFlag.SERVER_CUSTOMMSG_YLZG_SWITCHSCENE),onSwitchScene);
			_model.addEventListener(String(CustomToClientFlag.SERVER_CUSTOMMSG__FB_SCENE_TRANSFER),  autoFroundWay);
			
			_model.addEventListener( SceneEvent.SceneLoaded, sceneOK );
		}
		
		/**
		 *	开始跨场景移动
		 * @param sceneConfig	目标场景
		 * @param doorConfig	目标场景副本门点
		 * @param fbConfig
		 * 
		 */
		private function start( sceneConfig : String, doorConfig : String, fbConfig : String ):void
		{
			if(TipsWindowUI.getInstance().created==true)
			{
				return ; 	
			}
			SitCrossleggedLogic.getInstance().stopSit();
			
			//判断是否在副本
			if( GameData.SceneType == GameDefine.SceneType_CS )
			{
				if( SceneManager.getInstance().norSceneView.scenePlayerLogic.hasNpc() )
				{
					GameData.Role.startMove( [ new Point( 2400, 650 ) ]);
					
					return;
				}
//				else
//				{//提示是否离开副本
//					AlertUI.showAlert( "", "是否离开副本", AlertUI.OK|AlertUI.CANCEL, onClick );
//					
//					function onClick( type:int, str:String, clickData:* ):void
//					{
//						if( AlertUI.YES == type)
//						{
//							return
//						}
//					}
//				}
				return;
			}
			
			MainUIManager.getInstance().autoFoundWay	=	true;
			UIManage.getInstance().hideOpenedUI();
			_doorID			=	doorConfig;
			_npcModel		=	null;
			_roleModel		=	null;
			
			if(fbConfig != "0")
			{
				DuplicatesLogic.getInstance().FBSceneID(fbConfig,DuplicatesLogic.PK_FB);
			}
			if(String(GameData.SceneConfig) == sceneConfig)
			{//同场景
				_npcModel	=	SceneManager.getInstance().getConfigMC(doorConfig);
				if(null != _npcModel)
				{
					if(FunctionGuideUI.getInstance().stage) return;
					_npcModel.dispatchEvent( new MouseEvent( MouseEvent.MOUSE_DOWN ));
				}else
				{//等待加入
					_model.addEventListener( CustomEvent.ADD_NPC, checkNpc );
				}
			}else
			{	//在大地图上移动
				WorldMapLogic.getInstance().autoMove( sceneConfig );
				//等待添加对应门点资源
				_model.addEventListener( CustomEvent.ADD_NPC, checkNpc );
			}
		}
		private function autoFroundWay(e:CustomMsgEvent):void
		{
			var sceneID:String		=	e.pList[0];
			var npcID:String		=	e.pList[4];
			var doorIndex:String	=	e.pList[3];
			if(FunctionGuideUI.getInstance().stage) return;
			start(sceneID,npcID,doorIndex);
//			trace("dsffas")
		}
		/**
		 * 添加门点时检测是否为需要的门点 
		 * @param e
		 * 
		 */		
		private function checkNpc(e:CustomEvent):void
		{
			var checkModel : BasePlayerModel	=	null;
			checkModel = e.data[ 0 ];

			if( checkModel.playerObj.GetPropertiesEx( "ConfigID") == _doorID )
			{//门点有了
				_npcModel = checkModel;
				//等主角，当主角在舞台上的时候，发送点击门点消息，自己跑过去
				_roleModel = GameData.Role;
				gotoMc();
			}
//			else if( checkModel is RoleModel)
//			{
//				_roleModel	=	checkModel;
//			}
			
		}
		/**
		 * 场景加载完成 
		 * @param e
		 * 
		 */		
		private function sceneOK(e:SceneEvent):void
		{
			_isOver	=	true;
			gotoMc();
		}
		
		/**
		 * 切场景 
		 * @param e
		 * 
		 */		
		private function onSwitchScene(e:CustomMsgEvent):void
		{
			_isOver	=	false;
		}
		
		private function gotoMc():void
		{
			if( _roleModel != null && null != _npcModel  && _isOver)
			{
				//当点击的npc，移动的玩家都在舞台上了，删除事件
				_model.removeEventListener( CustomEvent.ADD_NPC, checkNpc);
				
				setTimeout( clickNPC, 500 );
			}
		}
		/**
		 * 开始模拟点击
		 * @param e
		 * 
		 */		
		public function clickNPC():void
		{
			if(FunctionGuideUI.getInstance().stage) return;
			GameData.Role.killMove();
			
			MainUIManager.getInstance().autoFoundWay	=	true;
			UIManage.getInstance().hideOpenedUI();
			_npcModel.dispatchEvent( new MouseEvent( MouseEvent.MOUSE_DOWN ));
			_npcModel	=	_roleModel	=	null;
		}
		
		/**
		 *停止引导 
		 */
		public function stopGuide():void
		{
			MainUIManager.getInstance().autoFoundWay = false;
			WalkManager.getInstance().walkLogic.clear();
			GameData.Role.status	=	PlayerActionFlag.NormalWait;
		}
	}
}